<?php  
class ControllerModuleSizeFilter extends Controller {
  public $optionId = 11;
  protected function index() {
    $this->language->load('module/sizefilter');
    
    $this->data['heading_title'] = $this->language->get('heading_title');
    
    if (isset($this->request->get['size_id'])) {
      $parts = $this->request->get['size_id'];
    } else {
      $parts = '';
    }
    $this->data['size_id'] = $parts;
    
    $this->load->model('catalog/option');
    $this->load->model('catalog/product');
    
    $option_info = $this->model_catalog_option->getOptionValueDescriptions($this->optionId);
    $this->data['categories'] = array();
          
    $url = http_build_query($this->request->get);

    foreach ($option_info as $key => $category) {
      $children_data = array();
      
      $data = array(
        'filter_size_id'  => $category['option_value_id'],
        'filter_sub_category' => true
      );    
        
//      $product_total = $this->model_catalog_product->getTotalProducts($data);
            
      $children_data[] = array(
        'size_id'    =>$category['option_value_id'],
        'name'        => $category['option_value_description'][2]['name'],
        'href'        => $this->url->link('product/category', 'path=' . $category['option_value_id'])
      );
      
      $data = array(
        'filter_category_id'  => $category['option_value_id'],
        'filter_sub_category' => true 
      );    
        
      $product_total = $this->model_catalog_product->getTotalProducts($data);

      if(empty($parts)){
        $urlFilter = HTTP_SERVER.'index.php?'.$url.(empty($url)?'':'&').'size_id='.$category['option_value_id'];
      } else {
        $urlReplace = str_replace('size_id='.$parts,'size_id='.$category['option_value_id'],$url);
        $urlFilter = HTTP_SERVER.'index.php?'.$urlReplace;
      } 
      $this->data['categories'][] = array(
        'size_id'    => $category['option_value_id'],
        'name'        => $category['option_value_description'][2]['name'],
        'children'    => $children_data,
        'href'        => $urlFilter
      );
    }
    
    if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/sizefilter.tpl')) {
      $this->template = $this->config->get('config_template') . '/template/module/sizefilter.tpl';
    } else {
      $this->template = 'default/template/module/sizefilter.tpl';
    }
    
    $this->render();
    }
}
?>